package com.jiyun.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jiyun.mapper.ItemMapper;
import com.jiyun.pojo.Item;
import com.jiyun.pojo.JsonRes;
import com.jiyun.service.ItemService;
import com.jiyun.utils.AliyunOSSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author: 刘清芳
 * @description: controller
 * @date: 2023/12/19 20:27
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("item")
public class ItemController {
    @Autowired
    ItemService itemService;
    @Autowired
    AliyunOSSClientUtil aliyunOSSClientUtil;
//展示

    @PostMapping("/findAll")
//    (required = false)
    public  List<Item> findAll(@RequestBody Item item){
        return itemService.findAll(item);
    }


//    名字查询
    @PostMapping("/findAllName")
    public List<Item>findAllName(){
        return itemService.findAllName();
    }

    //    上传图片
    @PostMapping("/upload")
    public JsonRes upload(@RequestParam("file") MultipartFile file) {
        String name;
        try {
            name = aliyunOSSClientUtil.uploadImg2Oss(file);
            String imgUrl = aliyunOSSClientUtil.getImgUrl(name);
            return JsonRes.success(imgUrl);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonRes.error("上传失败");
        }
    }
    @PostMapping("/add")
    public JsonRes add(@RequestBody Item item){
        itemService.saveOrUpdate(item);
        return JsonRes.success("操作成功");
    }

//    查询子集名称
    @PostMapping("/findById")
    public List<Item>findById(@RequestParam Integer id){
        return itemService.findById(id);
    }
//    逻辑删除
    @PostMapping("/deleteById")
    public JsonRes deleteById(Integer id){
        return itemService.deleteById(id);
    }
}
